<?php

/**
 * This is the model class for table "zy_custom".
 *
 * The followings are the available columns in table 'zy_custom':
 * @property integer $custom_id
 * @property string $custom_surname
 * @property string $telephone
 * @property string $email
 * @property integer $agent_number
 * @property string $custom_name
 * @property string $custom_Passport
 * @property integer $equipment_id
 * @property string $equipment_number
 * @property integer $package_id
 */
class Custom extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Custom the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_custom';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('reback_time,deposit','required'),
			array('agent_number,user_id,package_id,equipment_id,access_id,sale_id,payment_type,refund_type', 'numerical', 'integerOnly'=>true),
			array('custom_surname, telephone, password,email, custom_name,reback_address,address,custom_Passport,equipment_number', 'length', 'max'=>255),
			// The following rule is used by search().
			array('rebark_time,time', 'safe'),
			// Please remove those attributes that should not be searched.
			array('custom_id,user_id,password,custom_surname,deposit,telephone,sale_id, email, agent_number, custom_name, reback_time,reback_address,custom_Passport, equipment_id, equipment_number, package_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'equipment'=>array(self::HAS_MANY,'Equipment','equipment_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'custom_id' => Yii::t('base','Deposit'),
			'custom_surname' => Yii::t('base','Custom Surname'),
			'telephone' => Yii::t('base','telephone'),
			'email' => Yii::t('base','email'),
			'agent_number' => 'Agent Number',
			'custom_name' => Yii::t('base','Custom Name'),
			'custom_Passport' => Yii::t('base','Custom Passport'),
			'equipment_id' => Yii::t('base','Equipment Name'),
			'equipment_number' => Yii::t('base','Equipment Number'),
			'package_id' => Yii::t('base','Package Name'),
			 'user_id'=>Yii::t('base','User Name'),
			 'password'=>Yii::t('base','Password'),
			 'reback_time'=>Yii::t('base','Reback Time'),
			 'reback_address'=>Yii::t('base','Reback Address'),
			 'address'=>Yii::t('base','Address'),
			 'access_id'=>Yii::t('base','Access User'),
			'telephone_number'=>Yii::t('base','Telephone Number'),
			 'time'=>Yii::t('base','Time'),
			 'deposit'=>Yii::t('base','Custom Deposit'),
			 'create_time'=>Yii::t('base','Customer Create Time'),
			 'over_time'=>Yii::t('base','Customer Over Time'),
			  'sale_id'=>Yii::t('base','Sale User'),
			 'sale_address'=>Yii::t('base','Sale Address'),
				'isreback'=>Yii::t('base','isreback'),
				'payment_type'=>Yii::t('base','payment_type'),
				'refund_type'=>Yii::t('base','refund_type'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($results)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('custom_id',$this->custom_id);
		$criteria->addInCondition('equipment_number',$results);
		$criteria->compare('custom_surname',$this->custom_surname,true);
		$criteria->compare('telephone',$this->telephone,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('agent_number',$this->agent_number);
		$criteria->compare('custom_name',$this->custom_name,true);
		$criteria->compare('custom_Passport',$this->custom_Passport,true);
		$criteria->compare('equipment_id',$this->equipment_id,true);
		$criteria->compare('equipment_number',$this->equipment_number,true);
		$criteria->compare('package_id',$this->package_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('password',$this->password);
		$criteria->compare('reback_time',$this->reback_time,true);
		$criteria->compare('reback_address',$this->reback_address,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('access_id',$this->access_id,true);
		$criteria->compare('sale_id',$this->sale_id,true);
	//	$criteria->compare('deposit',$this->deposit,true);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'user_id desc',
				),
		));
	}
	public function search2()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
	
		$criteria->compare('custom_id',$this->custom_id);
		$criteria->compare('custom_surname',$this->custom_surname,true);
		$criteria->compare('telephone',$this->telephone,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('agent_number',$this->agent_number);
		$criteria->compare('custom_name',$this->custom_name,true);
		$criteria->compare('custom_Passport',$this->custom_Passport,true);
		$criteria->compare('equipment_id',$this->equipment_id,true);
		$criteria->compare('equipment_number',$this->equipment_number,true);
		$criteria->compare('package_id',$this->package_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('password',$this->password);
		$criteria->compare('reback_time',$this->reback_time,true);
		$criteria->compare('reback_address',$this->reback_address,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('access_id',$this->access_id,true);
	//	$criteria->compare('deposit',$this->deposit,true);
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'sort'=>array(
						'defaultOrder'=>'user_id desc',
				),
		));
	}
	public function search3($results){
		$criteria=new CDbCriteria;
		$criteria->compare('custom_surname',$this->custom_surname,true);
		$criteria->compare('custom_name',$this->custom_name,true);
		$criteria->compare('equipment_number',$this->equipment_number,true);
		$criteria->compare('sale_id',$this->sale_id,true);
		$criteria->addInCondition('custom_id',$results); 
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'sort'=>array(
						'defaultOrder'=>'custom_id desc',
				),
		));
	}
	public function getCustomId($user_id){
		$rs=Custom::model()->find('user_id='.$user_id);
		return $rs->custom_id;
	}
	//得到custom的用户名
	public function getCustomUserName($id)
	{
		$rs=User::model()->findByPk($id);
		return $rs->user_name;
	}
	public function getPackageName($id)
	{
		$rs=Package::model()->findByPk($id);
		return $rs->package_name;
	}
	public function getEquipmentName($id)
	{
		$rs=Equipment::model()->findByPk($id);
		return $rs->telephone_number;
	}
	public function deposit($id){
		$rs=UserService::model()->find('user_id='.$id.' AND service_id=12');
		return $rs->deposit;
	}
	public function getEquipmentSimCardByUserId($user_id){
		$rss=Custom::model()->find('user_id='.$user_id);
		$rs=Equipment::model()->findByPk($rss->equipment_id);
		return $rs->telephone_number;
	}
	public function getUserName($id)
	{
		$rs=User::model()->findByPk($id);
		return $rs->user_name;
	}
	public function isjudge($id)
	{
		$rs=UserService::model()->find('user_id='.$id.' AND service_id=12');
		return !$rs->isreback;
	}
	public function getflow($phone_number)
	{
		return leftCommonCounter($phone_number);
	}
	public function getsaleId()
	{ 
		$sql = "select * from zy_user where level = 6 ";
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($rs,'user_id','user_name');
		
	}
	//再次创建客户时 insert userservice
	public function reinsertuserservice($equipment_number){
		$equipment=Equipment::model()->find("equipment_number="."'$equipment_number'");
		$equipment_id=$equipment->equipment_id;
		//再次销售时userservice表的处理
		$userservice=UserService::model()->find("isused=0 AND isreback=1 AND equipment_number ='".$equipment_number."'");
		if($userservice!=""){
		if($userservice->user_id!=0){
			$userservice->isused=1;
			$userservice->access_id=0;
			$userservice->update();
			$rs2 = new UserService();
			$rs2->create_user=yii::app()->user->id;
			$rs2->access_id = $userservice->access_id;
			$rs2->equipment_id=$equipment_id;
			$rs2->equipment_number=$equipment_number;
			$rs2->isreback=1;
			$rs2->isused=0;
			$rs2->service_id = 12;
			return $rs2->save();
		}
		}
	}
	//用递归生成不重复的用户名
	public  function setuser()
	{
		$user_name=rand(10000000, 99999999);
		$user_result=User::model()->find('user_name='.$user_name);
		if($user_result!=""){
			setuser();
		}
		return $user_name;
	}
	// 创建中间平台账号
	public function CreateUser($password){
		$user_name=self::setuser();
		$telephone=$_POST['Custom']['telephone'];
		$email=$_POST['Custom']['email'];
		$custom_level="8";
		$sql="INSERT INTO zy_user (user_name,password,level,telephone,email) VALUES(:user_name,:password,:level,:telephone,:email)";
		$result = yii::app()->db->createCommand($sql);
		$result->bindParam(":user_name",$user_name);
		$result->bindParam(":password",$password);
		$result->bindParam(":level",$custom_level);
		$result->bindParam(":telephone",$telephone);
		$result->bindParam(":email",$email);
		$result->execute();
		return $user_name;
	}
	public function CreateLoginList($id){
		//给客户分配a2b帐号
		if($id==6){
			$package_details=PackageDetails::model()->find("package_type=:package_type AND isused =:isused",array(":package_type"=>'20',":isused"=>0));
		}elseif($id==8){
			$package_details=PackageDetails::model()->find("package_type=:package_type AND isused =:isused",array(":package_type"=>'100',":isused"=>0));
		}elseif($id==7){
			$package_details=PackageDetails::model()->find("package_type=:package_type AND isused =:isused",array(":package_type"=>'50',":isused"=>0));
		}
		elseif($id==9){
			$package_details=PackageDetails::model()->find("package_type=:package_type AND isused =:isused",array(":package_type"=>'10',":isused"=>0));
		}
		$package_details_id = $package_details->package_details_id;
		$field1=$package_details->name;
		$field2=$package_details->password;
		$field3="billing3.novatel.com.au";
		if($field1!=""){
			$userservice=UserService::model()->find('user_id=:user_id AND service_id=:service_id',array(':user_id'=>$user->user_id,':service_id'=>$service->service_id));
			$user_service_id=$userservice->user_service_id;
			$loginlist= new LoginList;
			$loginlist->user_service_id = $user_service_id;
			$loginlist->field1=$field1;
			$loginlist->field2=$field2;
			$loginlist->field3=$field3;
			$loginlist->save();
			$package_details_result=PackageDetails::model()->find('package_details_id='.$package_details_id);
			$package_details_result->isused=1;
			$package_details_result->update();
			return true;
		}
	}
	// 获得custom的信息
	public function getCustom($id){
		$custom=Custom::model()->find('user_id='.$id);
		return $custom;
	}
	//获得package的信息
	public function getPackage($id){
		$package=Package::model()->find('package_id='.$id);
		return $package;
	}
	//获得equipment的信息
	public function getEquipment($id){
		$equipment=Equipment::model()->find("equipment_number="."'$id'");
		return $equipment;
	}
	public function updateCustom($isreback,$user_id,$equipment_id,$city){
		if(yii::app()->user->level==1){
			$sale_id=Yii::app()->request->getParam('user_id');
		}else{
			$sale_id=yii::app()->user->id;
		}
		if($isreback==0){
			$create_time=date("Y-m-d H:i:s");
		}
		$custom_result=Custom::model()->find('user_id='.$user_id);
		$custom_result->equipment_id=$equipment_id;
		$custom_result->sale_id=$sale_id;
		$custom_result->sale_address=$city;
		$custom_result->create_time=$create_time;
		$custom_result->update();
		return $custom_result;
	}
	public function updateEquipment($equipment_id,$id){
		$equipment_result=Equipment::model()->find('equipment_id='.$equipment_id);
		$equipment_result->updater=$id;
		$equipment_result->is_used=1;
		$equipment_result->save();
		return $equipment_result;
	}
	//此处跟销售不一样的地方 此处是用insert 销售员的时候是update
	public function updateUserService($isreback,$deposit,$package_id,$user_id,$package_price,$equipment_id,$equipment_number){
		if($isreback==2){
			$isused = 0;
		}elseif($isreback==0){
			$isused = 1;
		}
		$userservice_result= new UserService;
		$userservice_result->equipment_id=$equipment_id;
		$userservice_result->deposit=$deposit;
		$userservice_result->package_id=$package_id;
		$userservice_result->user_id=$user_id;
		$userservice_result->package_price=$package_price;
		$userservice_result->equipment_number=$equipment_number;
		$userservice_result->isreback=$isreback;
		$userservice_result->isused=$isused;
		$userservice_result->service_id=12;
		$userservice_result->access_id=yii::app()->user->id;
		$userservice_result->save();
		$service = Service::model()->find("code="."'phone'");
		$result2=new UserService;
		$result2->user_id=$user_id;
		$result2->service_id=$service->service_id;
		$result2->save();
		return $userservice_result;
	}
	// 根据客户订购的套餐价格从代理商的信用额度中扣除(公司代理,个人代理)
	public function updatecredit($package_price,$isreback){
		if($isreback==0){
		if(yii::app()->user->level==4 || yii::app()->user->level==7){
			$agent=AgentCompany::model()->find('user_id='.yii::app()->user->id);
		}
		$agent->credit=$agent->credit-$package_price;
		return $agent->update();
		}
	}
	//记录销售数据
	public function createbonus($isreback,$package_id,$package_price,$user_service_id,$agent_number,$company_number){
		$agentcompany=AgentCompany::model()->find('user_id='.yii::app()->user->id);
		$agent_type=$agentcompany->agent_type;
		if($isreback==0){
			$sale_time=date('Y-m-d H:i:s');
			$user_id=yii::app()->user->id;
			$rate=Rate::model()->find('package_id='.$package_id.' AND agent_type='.$agent_type.' AND is_delete=0');
			$rate=$rate->rate;
			if($rate!=""){
			$bonus=($package_price * $rate)/100;
			$sql="INSERT INTO zy_bonus (package_id,package_price,sale_time,user_id,rate,bonus,user_service_id,agent_number,company_number) VALUES(:package_id,:package_price,:sale_time,:user_id,:rate,:bonus,:user_service_id,:agent_number,:company_number)";
			$result = yii::app()->db->createCommand($sql);
			$result->bindParam(":package_id",$package_id);
			$result->bindParam(":package_price",$package_price);
			$result->bindParam(":sale_time",$sale_time);
			$result->bindParam(":user_id",$user_id);
			$result->bindParam(":rate",$rate);
			$result->bindParam(":bonus",$bonus);
			$result->bindParam(":user_service_id",$user_service_id);
			$result->bindParam(":agent_number",$agent_number);
			$result->bindParam(":company_number",$company_number);
			$result->execute();
			return true;
			}
		}
			
	}
	// 根据当前的UserId获得agent_number和company_number
	public function getAgentNumber(){
		$user_id=yii::app()->user->id;
		$agentperson=AgentPerson::model()->find('user_id='.$user_id);
		return $agentperson;
	}
	//创建销售员下的提成表 --记录销售员套餐卖出情况 没有提成比例概念
	public function createsalebonus($package_id,$package_price,$user_id,$user_service_id){
		$sale_time=date("Y-m-d H:i:s");
		$sql="INSERT INTO zy_bonus (package_id,package_price,sale_time,user_id,user_service_id) VALUES(:package_id,:package_price,:sale_time,:user_id,:user_service_id)";
		$result = yii::app()->db->createCommand($sql);
		$result->bindParam(":package_id",$package_id);
		$result->bindParam(":package_price",$package_price);
		$result->bindParam(":sale_time",$sale_time);
		$result->bindParam(":user_id",$user_id);
		$result->bindParam(":user_service_id",$user_service_id);
		$result->execute();
		return true;
	}
	// 更新时根据custom_id得出sale_id
	public function getupdatesaleId(){
		$custom_id =yii::app()->request->getParam("id");
		$custom=self::model()->find('custom_id='.$custom_id);
		return $custom->sale_id;
	}
	public function getupdateUserServiceId($user_id,$equipment_number,$isreback){
		$userservice=UserService::model()->find("user_id=".$user_id." AND equipment_number="."'$equipment_number'"." AND isreback=".$isreback);
		return $userservice->user_service_id;
	}
}